﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AccesoDatos.Manejador;
using System.Runtime.InteropServices;

namespace RMA.Pantallas
{
    public partial class frmCreaOrdenServicio : Form
    {
        [DllImport("user32.dll")]
        static extern bool AnimateWindow(IntPtr hwnd, uint dwTime, uint dwFlags);

        public class WinApi
        {
            public static int GWL_EXSTYLE = -20;
            public static int WS_EX_LAYERED = 0x80000;
            public static int LWA_ALPHA = 0x2;
            [DllImport("user32")]
            public static extern int SetLayeredWindowAttributes(IntPtr hWnd, byte crey, byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int GetLayeredWindowAttributes(IntPtr hWnd, byte crey, out byte alpha, int dwFlags);
            [DllImport("user32")]
            public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
            [DllImport("user32")]
            public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        }
        void mkwndtrans(IntPtr hWnd, int num)
        {
            int oldWinStyle = WinApi.GetWindowLong(hWnd, WinApi.GWL_EXSTYLE);
            WinApi.SetWindowLong(hWnd, WinApi.GWL_EXSTYLE,
            num < 255 ? oldWinStyle | WinApi.WS_EX_LAYERED :
            oldWinStyle & WinApi.WS_EX_LAYERED);
            WinApi.SetLayeredWindowAttributes(hWnd, 0, (byte)num, WinApi.LWA_ALPHA);
        }
        enum AnimateWindowFlags : uint
        {
            AW_HOR_POSITIVE = 0x00000007,
            AW_HOR_NEGATIVE = 0x00000002,
            AW_VER_POSITIVE = 0x00000004,
            AW_VER_NEGATIVE = 0x00000008,
            AW_CENTER = 0x00000010,
            AW_HIDE = 0x00010000,
            AW_ACTIVATE = 0x00020000,
            AW_SLIDE = 0x00040000,
            AW_BLEND = 0x00080000
        }

        string strLLave = "0";
        DataTable dtUser = null;
        DataTable dtResultados = null;
        public frmCreaOrdenServicio()
        {
            InitializeComponent();
        }
        public frmCreaOrdenServicio(DataTable _dtUser)
        {
            dtUser = _dtUser;
            InitializeComponent();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (txtBusqueda.Text != "")
            {
                
                Manejador_OrdenServicio objOrdenServ = new Manejador_OrdenServicio();

                if (rbtnFactura.Checked)
                    strLLave = "1";
                if (rbtnIdCliente.Checked)
                    strLLave = "2";
                if (rbtnNombreCte.Checked)
                    strLLave = "3";
                try
                {
                    dgvBusqueda.DataSource = null;
                    spinningProgress1.Visible = true;
                    backgroundWorker1.RunWorkerAsync();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Ingrese el Criterio de Busqueda para continuar", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtBusqueda.Focus();
            }
            

        }

        private void rbtnFactura_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnFactura.Checked)
            {
                strLLave = "1";
                rbtnIdCliente.Checked = false;
                rbtnNombreCte.Checked = false;
                txtBusqueda.Focus();
            }
            else
            {
                if (rbtnIdCliente.Checked)
                {
                    strLLave = "2";
                    rbtnFactura.Checked = false;
                    rbtnNombreCte.Checked = false;
                    txtBusqueda.Focus();

                }
                else
                {
                    if (rbtnNombreCte.Checked)
                    {
                        strLLave = "3";
                        rbtnIdCliente.Checked = false;
                        rbtnFactura.Checked = false;
                        txtBusqueda.Focus();
                    }
                }
            }
        }

        private void rbtnIdCliente_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnIdCliente.Checked)
            {
                strLLave = "2";
                rbtnFactura.Checked = false;
                rbtnNombreCte.Checked = false;
                txtBusqueda.Focus();

            }
            else
            {
                if (rbtnFactura.Checked)
                {
                    strLLave = "1";
                    rbtnIdCliente.Checked = false;
                    rbtnNombreCte.Checked = false;
                    txtBusqueda.Focus();
                }
                else
                {
                    if (rbtnNombreCte.Checked)
                    {
                        strLLave = "3";
                        rbtnIdCliente.Checked = false;
                        rbtnFactura.Checked = false;
                        txtBusqueda.Focus();
                    }
                }
            }
        }

        private void rbtnNombreCte_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnNombreCte.Checked)
            {
                strLLave = "3";
                rbtnIdCliente.Checked = false;
                rbtnFactura.Checked = false;
                txtBusqueda.Focus();
            }
            else
            {
                if (rbtnIdCliente.Checked)
                {
                    strLLave = "2";
                    rbtnFactura.Checked = false;
                    rbtnNombreCte.Checked = false;
                    txtBusqueda.Focus();

                }
                else
                {
                    if (rbtnFactura.Checked)
                    {
                        strLLave = "1";
                        rbtnIdCliente.Checked = false;
                        rbtnNombreCte.Checked = false;
                        txtBusqueda.Focus();
                    }
                }
            }
        }

        private void frmCreaOrdenServicio_Load(object sender, EventArgs e)
        {
            this.BringToFront();
            txtBusqueda.Focus();
        }

        private void dgvBusqueda_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Manejador_OrdenServicio objOrdenServ = new Manejador_OrdenServicio();
            DataTable dtDetalleFact = null;
            string strFolId = dgvBusqueda.Rows[e.RowIndex].Cells[2].Value.ToString();
            DateTime Fecha = Convert.ToDateTime(dgvBusqueda.Rows[e.RowIndex].Cells[1].Value.ToString());
            string strCliente = dgvBusqueda.Rows[e.RowIndex].Cells[0].Value.ToString();


            try
            {
                dtDetalleFact = objOrdenServ.RegresaDetalleFactura(strFolId);
                frmDetalleFactura frmD = new frmDetalleFactura(dtDetalleFact, strFolId, Fecha, strCliente);
                AnimateWindow(frmD.Handle, 250, (uint)AnimateWindowFlags.AW_CENTER);
                frmD.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            Manejador_OrdenServicio objOrdenServ = new Manejador_OrdenServicio();
            dtResultados = objOrdenServ.RegresaBusquedaPrincipal(strLLave, txtBusqueda.Text);
            
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (dtResultados.Rows.Count > 0)
            {
                dgvBusqueda.DataSource = dtResultados;
                spinningProgress1.Visible = false;
                foreach (DataGridViewColumn ca in dgvBusqueda.Columns)
                {
                    ca.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
                
            }
            else
            {
                MessageBox.Show("No hay datos que mostrar", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtBusqueda.Focus();
                dgvBusqueda.DataSource = null;
            }
        }
    }
}
